<script>
	export default {
		data() {
			return {
				content: '',
				messageId: '',
				roomNumber:'',
				homeName:'',
				testJson: {
					messageId: '',
					content:'',
				},
			}
		},
		onLaunch: function() {
			const HC_names = uni.getStorageSync('HC_name');
			if(HC_names!=''){
				uni.switchTab({
					url: '/pages/index/index'
				});
			}else{
				uni.showToast({
					title: '请先登录',
					mask: true,
					icon: "none"
				
				});
			}
			// #ifdef APP-PLUS
			this.initialization();
			// #endif
			// this.getUser()
			console.log('App Launch')
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		globalData: {
			testJson: {
				messageId: '',
				content: '',
			},
			// 监听到有新服务器消息
			getSocketMsg(reData) {
				// 监听到服务器消息
				// console.log('收到服务器消息', reData);
				const data = JSON.parse(reData)
				try {
					const data1 = JSON.parse(data.msg)
					 if (data.msgType=="pension.com.alarm.entity.Alarm") {
						  console.log('收到服务器消息', reData);
						    this.homeName=data1.houseNo
							if(data1.devName=="sos报警"){
								this.content =data1.memberName+data1.alarmDetail
							}else{
								this.content =data1.houseNo+data1.devName+data1.alarmDetail
							}
							if(data1.status=="1"){
								this.content=this.content+"已解除警告"
							}
					}else {
						return
					}
					this.msgType=data.msgType
				} catch (e) {
					// console.log('收到错误消息', e);
				}
				if (data.code == "99") {
					if (data.msg != 'ping') {
						const msgs=JSON.parse(data.msg)
						console.log(msgs);
						if (msgs.alarmCategory == '02') {
						this.roomNumber=uni.getStorageSync("roomNumber")
						 console.log(this.homeName,this.roomNumber);
						if(this.homeName==this.roomNumber){
						this.testJson.messageId = this.messageId
						this.testJson.content = this.content
						// #ifdef APP-PLUS
						const module = uni.requireNativePlugin("WT-PushModule");
						console.log(module)
						const n = module.appPush({
							notificationArray: this.testJson
						});
						console.log(n)
						// #endif
						}
						}
					}
				}
			},
		},
		methods:{
			initialization(){
			
			//获取当前Activity
			const main = plus.android.runtimeMainActivity();
			//通过反射获取Android的Intent对象
			const Intent = plus.android.importClass("android.content.Intent");
			//通过宿主上下文创建 intent
			const intent = new Intent(main.getIntent());
			// io.dcloud.application.activity.DeviceControlPanelActivity
			//设置要开启的Activity包类路径  com.lcvc.xgx.yyzxyq.activity.DeviceControlPanelActivity换掉你自己的界面
			intent.setClassName(main, "com.lcvc.xgx.yyzxyq.activity.TuyaSystemActivity");
			//开启新的任务栈 （跨进程）
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			
			//开启新的界面
			main.startActivity(intent);
			//请求码保证了，开始的新界面和返回的是同一个操作
			var CODE_REQUEST = 1000
			//采用startActivityForResult开启新的界面，当界面关闭时可以处理返回结果， CODE_REQUEST请求码是唯一标识
			main.startActivityForResult(intent, CODE_REQUEST);
			
			
				// uni.showLoading({
				// 	title: '涂鸦智能初始化中'
				// });
				
				// try {
				// 	const module = uni.requireNativePlugin("LJY-SystemModule") //调用android中自定义的插件名称
				// 	//跳转到涂鸦设备详情
				// 	const ret=module.systemInit();
					
				// 	uni.hideLoading(); //加载结束
				// } catch (e) { //如果涂鸦智能无法连接
				// 	uni.hideLoading(); //加载结束
				// }
			},
			notificationToGo() {
				if (plus.runtime.arguments != "" || plus.runtime.arguments != null) {
					const androidIntent = JSON.parse(plus.runtime.arguments);
					const NotificationUrl = Object.values(androidIntent);
					console.log(NotificationUrl)
					uni.navigateTo({
						url: NotificationUrl[0]
					});
					plus.runtime.arguments = null
			
					this.getUser();
			
				}
			},
			
			//通知推送
			async getUser() {
				const res = await this
					.$myRequest({ //await 关键字 只能放在 async 函数内部， await关键字的作用 就是获取 Promise中返回的内容， 获取的是Promise函数中resolve或者reject的值
						url: '/api-portal/currentUser'
					})
				if (res.success == false) { //如果读取信息成功
					
				} else if (res.success == true) {
					console.log(res.body.currentUser.id)
					this.id = res.body.currentUser.id;
					this.checkopenSocket()
				}
			},
			//判断是否已连接
			checkopenSocket() {
				uni.sendSocketMessage({
					data: "ping",
					success: res => {
						return
					},
					fail: err => {
						this.socket()
					}
				});
			},
			//打开连接
			socket() {
				uni.connectSocket({
					url: 'ws://pm.gxtimeloit.com:10034/websocket/' + this.id + '/all',
					// url: 'ws://pm.gxtimeloit.com:10034/websocket/2/all',
					// url: 'ws://icewould.tpddns.cn:18123',
					success: data => {
						console.log(data)
					}
				});
				this.onSocketMessage()
			
			},
			//	打开成功监听服务器返回的消息
			onSocketMessage() {
				// 消息
				this.timeout = 30000;
				this.timeoutObj = null;
				uni.onSocketMessage(res => {
					this.getSocketMsg(res.data); // 监听到有新服务器消息
				});
			},
			// 监听到有新服务器消息
			getSocketMsg(reData) {
				// 监听到服务器消息
				console.log('收到服务器消息', reData);
				const data = JSON.parse(reData)
				try {
					const data1 = JSON.parse(data.msg)
					if (data1.hasOwnProperty("memberName")) {
						this.homeName=data1.houseNo
						this.content = data1.address + data1.memberName + data1.alarmDetail
					} else if (data1[0].hasOwnProperty("homeName")) {
						this.homeName=data1[0].homeName
						this.content =data1[0].homeName+data1[0].name+data1[0].codeText+data1[0].valueText
					}
				} catch (e) {
					// console.log('收到错误消息', e);
				}
				if (data.code == "99" ) {
					if (data.msg != 'ping') {
						this.roomNumber=uni.getStorageSync("roomNumber")
						if(this.homeName==this.roomNumber){
						// this.testJson.push({
						// 	// 'messageId': this.messageId,
						// 	'content': this.content,
						// })
						this.testJson['content']=this.content
						console.log(this.testJson)
						//#ifdef APP-PLUS
						this.NotificationPush()
						// #endif
						}
					}	
				}
				
			},
		}
	}
</script>

<style>
	/*每个页面公共css */
	/* 背景颜色 #EBEFF6 */
	page {
		background-color: #F3F3F3;
	}
	/* 苹方字体 */
	@font-face {
		font-family: 'ppf';
		src: url('~@/static/font/pf.ttf');
	}
	
	* {
		font-family: 'ppf';
	}
	
	.uni-icons span {
		font-family: UniIcons;
	}
	/* 提交按钮样式 */
	.sub_button {
		height: 38px;
		width: 90.6%;
		background-color: #da422a;
		color: #FFFFFF;
		line-height: 38px;
		text-align: center;
		margin: 48px auto 0 auto;
		border-radius: unset;
	}
</style>
